:Init

@setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@pushd %~dp0
@prompt $_
@echo off

if "%2" == "/q" set _cloudDriveOverwrite=True
if "%2" == "-q" set _cloudDriveOverwrite=True


if "%1"=="" goto Usage
goto %1

:Usage
@echo.
@echo Simplifies the process of creating a local cloud drive containing 
@echo application source and publishing to Azure blob storage for use. 
@echo.
@echo PUBLISHAPPLICATION.CMD AppKey [/q]
@echo.
@echo   /q      Quiet mode: overwrites any previous local virtual drive 
@echo           or target cloud drive without asking for confirmation.
@echo                 
@echo   AppKey  Valid declared application section. Example sections
@echo           include:
@echo.
@echo               IIS
@echo               PHP
@echo               Apache
@echo               Umbraco    (for Umbraco CMS)
@echo               ScrewTurn  (for ScrewTurn Wiki)
@echo.
@echo Edit the 'PublishApplication.cmd' file to view declared application,
@echo alter defaults or to create customized cloud drive uploads for 
@echo specific requirements.
@echo.
@goto End

:EditRequired
@echo.
@echo Please edit the 'PublishApplication.cmd' file.
@echo.
@echo Update the default settings with values appropriate for your
@echo deployment.
@echo.
@goto End

REM ~~( IIS )~~
:IIS
if "%_cloudDriveLetter%"       == "" set _cloudDriveLetter=I
if "%_cloudDriveBlobUri%"      == "" set _cloudDriveBlobUri=wa-accelerator-drives/IisDrive.vhd
if "%_cloudDriveFile%"         == "" set _cloudDriveFile=IisDrive.vhd
if "%_cloudDriveSize%"         == "" set _cloudDriveSize=512
if "%_cloudDriveTargetFolder%" == "" set _cloudDriveTargetFolder=\
if "%_cloudDriveSource%"       == "" set _cloudDriveSource="%SystemDrive%\inetpub\wwwroot"
if exist "%_cloudDriveSource%" goto Start
@echo IIS not found at '%_cloudDriveSource%'.
goto EditRequired

REM ~~( WPI )~~
:WPI
if "%_cloudDriveLetter%"       == "" set _cloudDriveLetter=W
if "%_cloudDriveBlobUri%"      == "" set _cloudDriveBlobUri=wa-accelerator-drives/WpiDrive.vhd
if "%_cloudDriveFile%"         == "" set _cloudDriveFile=WpiDrive.vhd
if "%_cloudDriveSize%"         == "" set _cloudDriveSize=512
if "%_cloudDriveTargetFolder%" == "" set _cloudDriveTargetFolder=\Wpi
if "%_cloudDriveSource%"       == "" set _cloudDriveSource="%SystemDrive%\inetpub\accelerators"
if exist "%_cloudDriveSource%" goto Start
@echo IIS not found at '%_cloudDriveSource%'.
goto EditRequired

REM ~~( UMBRACO )~~
:UMBRACO
if "%_cloudDriveLetter%"       == "" set _cloudDriveLetter=U
if "%_cloudDriveSize%"         == "" set _cloudDriveSize=512
if "%_cloudDriveTargetFolder%" == "" set _cloudDriveTargetFolder=\
set _umbracoDataDir=%_cloudDriveSource%\umbraco\data
goto IIS

REM ~~( SCREWTURN WIKI )~~
:SCREWTURN
if "%_cloudDriveFile%"         == "" set _cloudDriveFile=WikiDrive.vhd
if "%_cloudDriveLetter%"       == "" set _cloudDriveLetter=W
if "%_cloudDriveSize%"         == "" set _cloudDriveSize=256
if "%_cloudDriveTargetFolder%" == "" set _cloudDriveTargetFolder=\
goto IIS

REM ~~( APACHE )~~
:APACHE
set _cloudDriveLetter=O
set _cloudDriveBlobUri=wa-accelerator-drives/ApacheDrive.vhd
set _cloudDriveFile=ApacheDrive.vhd
set _cloudDriveSize=64
set _cloudDriveTargetFolder=\Apache
set _cloudDriveSource=%ApacheHome%
if exist "%_cloudDriveSource%" goto Start
set _cloudDriveSource=%ProgramFiles%\Apache Software Foundation\Apache2.2
if exist "%_cloudDriveSource%" goto Start
@echo Apache not found at '%_cloudDriveSource%'.
set _cloudDriveSource=%ProgramFiles(x86)%\Apache Software Foundation\Apache2.2
if exist "%_cloudDriveSource%" goto Start
@echo Apache not found at '%_cloudDriveSource%'.
goto EditRequired

REM ~~( PHP )~~
:PHP
set _cloudDriveLetter=P
set _cloudDriveBlobUri=wa-accelerator-drives/PhpDrive.vhd
set _cloudDriveFile=PhpDrive.vhd
set _cloudDriveSize=64
set _cloudDriveTargetFolder=\Php
set _cloudDriveSource=%ProgramFiles%\PHP
if exist "%_cloudDriveSource%" goto Start
@echo PHP not found at '%_cloudDriveSource%'.
set _cloudDriveSource=%ProgramFiles(x86)%\PHP
if exist "%_cloudDriveSource%" goto Start
@echo PHP not found at '%_cloudDriveSource%'.
goto EditRequired

REM ~~( START )~~
:Start
@rem
@rem START
@rem
@rem Sections below contain the common actions to perform for 
@rem validation, creation and upload of a local source folder
@rem to a page blob as a cloud drive.
@rem

REM ~~( VALIDATION )~~
:CommonValidation
@echo.
@echo Configuration Settings
@echo.
@echo   Azure URI:       "%_cloudDriveBlobUri%"      
@echo   VHD Temp Drive:  "%_cloudDriveLetter%"       
@echo   VHD Source:      "%_cloudDriveSource%"       
@echo   VHD File:        "%_cloudDriveFile%"         
@echo   VHD Size:        "%_cloudDriveSize%"         
@echo   VHD Folder:      "%_cloudDriveTargetFolder%" 
@echo   VHD Overwrite:   "%_cloudDriveOverwrite%"
@echo.

REM ~~( CHECKDRIVE )~~
:CheckDrive
if exist "%_cloudDriveLetter%:" (
    @echo.
    @echo ERROR:  Drive letter '%_cloudDriveLetter%' already in use.
    @echo.
    @goto EditRequired
)
@echo.
@echo Drive letter '%_cloudDriveLetter%' is available.
@echo.

REM ~~( CHECKVHD )~~
:CheckVHD
if exist "%_cloudDriveFile%" (
    @echo.
    @echo WARNING:  Existing file found at '%_cloudDriveFile%'.
    @echo.
    if "%_cloudDriveOverwrite%"=="" (
        choice /C YN /M "File already exists '%_cloudDriveFile%', overwrite"
        if not ERRORLEVEL 1 goto EditRequired
    )
    @del.exe "%_cloudDriveFile%"
)

REM ~~( CREATEDRIVE )~~
:CreateDrive
@echo.
@echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo.
@echo ACCELERATOR NOTE:
@echo.
@echo IGNORE the request to FORMAT the new drive. No action
@echo will be required; this will be done by the scripts.
@echo.
@echo All POPUP windows can be SAFELY CLOSED at any time.
@echo.
@echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ping 1.0.0.0 -n 5 -w 1000 >NUL
@echo.
@echo Creating local Virtual Hard Drive (VHD) image.
@echo.
REM x| @call Resources\createVhd.cmd "%_cloudDriveFile%" %_cloudDriveSize% %_cloudDriveLetter%
@echo create vdisk file="%_cloudDriveFile%" maximum=%_cloudDriveSize% type=fixed > "%TEMP%\accelcon.createdrive.script.txt"
@echo select vdisk file="%_cloudDriveFile%" >> "%TEMP%\accelcon.createdrive.script.txt"
@echo attach vdisk >> "%TEMP%\accelcon.createdrive.script.txt"
@echo create partition primary >> "%TEMP%\accelcon.createdrive.script.txt"
@echo assign letter=%_cloudDriveLetter% >> "%TEMP%\accelcon.createdrive.script.txt"
@echo format fs=ntfs label=vhd quick >> "%TEMP%\accelcon.createdrive.script.txt"
@diskpart.exe -s "%TEMP%\accelcon.createdrive.script.txt"
@ping 1.0.0.0 -n 5 -w 1000 >NUL

REM ~~( CREATETARGET )~~
:CreateTarget
set _cloudDriveTarget=%_cloudDriveLetter%:%_cloudDriveTargetFolder%
if exist "%_cloudDriveTarget%" goto CopyFiles
@echo.
@echo Creating target folder '%_cloudDriveTarget%'.
@echo.
@md.exe "%_cloudDriveTarget%"
if not errorlevel 0 goto Abort

REM ~~( COPYFILES )~~
:CopyFiles
@echo.
@echo Copying web site files to virtual drive...
@echo.
echo xcopy.exe "%_cloudDriveSource%" "%_cloudDriveTarget%" /e /y >> "%TEMP%\accelcon.debug.txt"
@xcopy.exe "%_cloudDriveSource%" "%_cloudDriveTarget%" /e /y >> "%TEMP%\accelcon.debug.txt"
if not errorlevel 0 goto Abort

REM ~~( SETPERMISSIONS )~~
:SetPermissions
@echo.
@echo Setting permissions on virtual drive files...
@echo.
echo cacls.exe "%_cloudDriveLetter%:" /T /E /G Everyone:F >> "%TEMP%\accelcon.debug.txt"
@cacls.exe "%_cloudDriveLetter%:" /T /E /G Everyone:F >> "%TEMP%\accelcon.debug.txt"
if not errorlevel 0 goto Abort

REM ~~( UNMOUNTDRIVE )~~
:UnmountDrive
@echo.
@echo Unmounting local virtual drive '%_cloudDriveLetter%:'.
@echo.
REM x| @call Resources\unmountVhd.cmd "%_cloudDriveFile%"
echo select vdisk file="%_cloudDriveFile%" > "%TEMP%\accelcon.unmount.script.txt"
echo detach vdisk >> "%TEMP%\accelcon.unmount.script.txt"
@diskpart.exe -s %TEMP%\accelcon.unmount.script.txt

REM ~~( UPLOADDRIVE )~~
:UploadDrive
if exist "%_cloudDriveSkipUpload%" goto Unmount
@echo.
@echo Uploading local virtual drive image to Windows Azure...
@echo.
@accelcon.exe /u /v %2 "%_cloudDriveFile%" "%_cloudDriveBlobUri%"
goto End

REM ~~( ABORT )~~
:Abort
@echo.
@echo ERROR: Aborting upload processing.
@echo.
@echo Unmounting local virtual drive '%_cloudDriveLetter%:'.
@echo.

REM ~~( UNMOUNT )~~
:Unmount
echo select vdisk file="%_cloudDriveFile%" > "%TEMP%\accelcon.unmount.script.txt"
echo detach vdisk >> "%TEMP%\accelcon.unmount.script.txt"
@diskpart.exe -s "%TEMP%\accelcon.unmount.script.txt"

REM ~~( END )~~
@echo.
@echo Completed.
@echo.
:End